package asia.lxq.cic.controller;

import asia.lxq.cic.config.GlobalConfig;
import asia.lxq.cic.entity.Activity;
import asia.lxq.cic.entity.Result;
import asia.lxq.cic.entity.Sign;
import asia.lxq.cic.entity.User;
import asia.lxq.cic.service.ActivityService;
import asia.lxq.cic.service.SignService;
import asia.lxq.cic.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/activity")
public class ActivityController {

    @Autowired
    ActivityService activityService;
    @Autowired
    SignService signService;
    @Autowired
    RedisUtil redisUtil;

    @GetMapping("/is_sign")
    public Result<Sign> isSign(int activityId, Authentication authentication){
        Sign sign = activityService.isSign(Integer.parseInt(authentication.getPrincipal() + ""), activityId);
        return Result.success(sign);
    }

    @GetMapping("/is_allow")
    public Result<Boolean> isAllow(int activityId,Authentication authentication){
        return activityService.isAllow(activityId,Integer.parseInt(authentication.getPrincipal() + ""));
    }

    @GetMapping("/activity_sign")
    public Result<Boolean> signActivity(int activityId,String code,Authentication authentication){
        return signService.signActivity(activityId,Integer.parseInt(authentication.getPrincipal() + ""),code);
    }

    @GetMapping("/activity_appeal")
    public Result<Sign> engageActivity(int activityId, String appealText, Authentication authentication){
        int userId = Integer.parseInt(authentication.getPrincipal() + "");
        return activityService.engageActivity(userId, activityId, appealText);
    }

    @GetMapping("/activity_own")
    public Result<Object> getOwnActivity(@RequestParam(required = false,defaultValue = "1") int page,
                                         @RequestParam(required = false,defaultValue = "10")int pageSize,
                                         Authentication authentication){
        int userId = Integer.parseInt(authentication.getPrincipal() + "");
        return activityService.getOwnActivity(userId, page, pageSize);
    }
    @GetMapping("/activity_engage")
    public Result<Object> getEngageActivity(@RequestParam(required = false,defaultValue = "1") int page,
                                         @RequestParam(required = false,defaultValue = "10")int pageSize,
                                         Authentication authentication){
        int userId = Integer.parseInt(authentication.getPrincipal() + "");
        return activityService.getEngageActivity(userId, page, pageSize);
    }

    //todo 需要判断当前用户是否是这个活动的发起人
    @PostMapping("/activity_alter")
    public Result<Activity> alterActivity(@RequestBody Activity activity){
        return Result.success(activityService.alterActivity(activity));
    }

    @PostMapping("/activity_propose")
    public Result<Object> activityPropose(@RequestBody Activity activity,Authentication authentication){
        activity.setActivityId(0);
        int userId = Integer.parseInt(authentication.getPrincipal() + "");
        User user = (User) redisUtil.get(GlobalConfig.PRE_USERINFO_REDIS + userId);
        activity.setActivitySponsorId(userId);
        activity.setActivityCollegeId(user.getCollegeId());
        activity.setActivityGroupId(user.getGroupId());
        activityService.addActivity(activity);
        return Result.success(activity);
    }

    @PostMapping("/activity_report")
    public Result<Boolean> reportActivity(@RequestBody Map<String,String> requestMap, Authentication authentication){
        String reportContent = requestMap.get("reportContent");
        int userId = Integer.parseInt(authentication.getPrincipal() + "");
        int activityId = Integer.parseInt(requestMap.get("activityId"));
        return signService.reportActivity(activityId,userId,reportContent);
    }

}
